﻿#region Header

/*
Behavioral Rating of Dancing Human Crowds based on Motion Patterns
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Text;
using paravili.Services;
using Sebarf.Services.Interfaces;

#endregion

namespace paravili.Steps {
	/// <summary>
	/// stores all values, hold by the <see cref="IStatisticValuesLocatorService"/>, as csv file to the disk
	/// </summary>
	public class StoreStatisticDataToDisk : ProcessStepWithMeasurement<Object> {
		#region Public Properties

		[ConfigurabelStringValue(Name = "Filename")]
		public string FileName {
			get { return m_fileName; }
			set {
				m_fileName = value;
				OpenFileforWrting(value);
			}
		}

		private void OpenFileforWrting(string value) {
			if (m_file != null) {
				m_file.Close();
			}
			try {
				if (File.Exists(m_fileName)) {
					File.Copy(m_fileName, Guid.NewGuid() + ".csv");
					File.Delete(m_fileName);
				}
				m_file = File.AppendText(value);
			}
			catch (Exception ex) {
			}
		}

		[ServiceRequest]
		public IResetHandlerService ResetHandlerService {
			set { value.Reset += OnReset; }
		}

		[ServiceRequest]
		public IStatisticValuesLocatorService StatisticValuesLocatorService { get; set; }

		[ConfigurabelBooleanValue(Name = "IsEnable")]
		public bool IsEnable { get; set; }

		#endregion

		#region Public Methods

		public StoreStatisticDataToDisk() {
			FileName = Directory.GetCurrentDirectory() + "\\" + "StatisticData.csv";
			;
		}

		protected override object OnProcess(object toProcess) {
			if (IsEnable && m_file != null) {
				ObservableCollection<StatisticValueEntry> img = StatisticValuesLocatorService.StatisticsValues;
				var builder = new StringBuilder();
				if (!m_isHeaderWritten) {
					foreach (StatisticValueEntry item in img) {
						builder.Append(item.Name);
						builder.Append(";");
					}
					builder.AppendLine();
					m_isHeaderWritten = true;
					m_countStatisticsValues = img.Count;
				}

				foreach (StatisticValueEntry item in img) {
					builder.Append(item.Value);
					builder.Append(";");
				}
				m_file.WriteLine(builder.ToString());
				m_file.Flush();
				if (m_countStatisticsValues != img.Count) {
					m_isHeaderWritten = false;
				}
				m_countStatisticsValues = img.Count;
				//(OutputPath + "\\" + (m_index++).ToString("00000\\.jpg"));
			}
			return toProcess;
		}

		#endregion

		#region Private Methods

		private void OnReset(object sender, EventArgs e) {
			if (m_file != null) {
				try {
					m_file.Flush();
					m_file.Close();
				}
				catch {

				}
				m_isHeaderWritten = false;
				OpenFileforWrting(m_fileName);
			}
		}

		#endregion

		#region Private Fields

		private int m_countStatisticsValues;
		private StreamWriter m_file;
		private string m_fileName;
		private bool m_isHeaderWritten;

		#endregion
	}
}